package com.hdu.treeUtils;

import java.util.Comparator;
import java.util.List;
import java.util.Objects;

import static java.util.Arrays.asList;

public class Test {

    public static void main(String[] args) {

        People people1 = People.of(0L, -1L, 1, "老板");
        People people2 = People.of(1L, 0L, 2, "经理1");
        People people3 = People.of(2L, 0L, 1, "经理2");
        People people4 = People.of(3L, 1L, 2, "项目经理1");
        People people5 = People.of(4L, 1L, 1, "项目经理2");
        People people6 = People.of(5L, 2L, 2, "项目经理3");
        People people7 = People.of(6L, 2L, 1, "项目经理4");
        People people8 = People.of(7L, 3L, 2, "普工1");
        People people9 = People.of(8L, 3L, 1, "普工2");
        List<People> peoples = asList(people1, people2, people3, people4, people5, people6, people7, people8, people9);

        List<People> treePeople = TreeUtils.buildTree(
                peoples,
                people -> people.getPId().equals(-1L),
                (parent, node) -> Objects.equals(parent.getId(), node.getPId()),
                People::setSubMenus
        );

        // 先序遍历
        System.out.println("先序遍历========");
        TreeUtils.preOrder(treePeople, (people -> System.out.println(people.getName())), People::getSubMenus);
        // 层序遍历
        System.out.println("层序遍历========");
        TreeUtils.levelOrder(treePeople, (people -> System.out.println(people.getName())), People::getSubMenus);
        // 后序遍历
        System.out.println("后序遍历========");
        TreeUtils.postOrder(treePeople, (people -> System.out.println(people.getName())), People::getSubMenus);
        // 同一层按照rank升序
        System.out.println("同层按照rank升序========");
        TreeUtils.sort(treePeople, Comparator.comparingInt(People::getRank), People::getSubMenus);
        TreeUtils.levelOrder(treePeople, (System.out::println), People::getSubMenus);

        //先序遍历========
        //老板
        //经理1
        //项目经理1
        //普工1
        //普工2
        //项目经理2
        //经理2
        //项目经理3
        //项目经理4
        //层序遍历========
        //老板
        //经理1
        //经理2
        //项目经理1
        //项目经理2
        //项目经理3
        //项目经理4
        //普工1
        //普工2
        //后序遍历========
        //普工1
        //普工2
        //项目经理1
        //项目经理2
        //经理1
        //项目经理3
        //项目经理4
        //经理2
        //老板
        //同层按照rank升序========
        //People(id=0, pId=-1, name=老板, rank=1, subMenus=[People(id=2, pId=0, name=经理2, rank=1, subMenus=[People(id=6, pId=2, name=项目经理4, rank=1, subMenus=[]), People(id=5, pId=2, name=项目经理3, rank=2, subMenus=[])]), People(id=1, pId=0, name=经理1, rank=2, subMenus=[People(id=4, pId=1, name=项目经理2, rank=1, subMenus=[]), People(id=3, pId=1, name=项目经理1, rank=2, subMenus=[People(id=8, pId=3, name=普工2, rank=1, subMenus=[]), People(id=7, pId=3, name=普工1, rank=2, subMenus=[])])])])
        //People(id=2, pId=0, name=经理2, rank=1, subMenus=[People(id=6, pId=2, name=项目经理4, rank=1, subMenus=[]), People(id=5, pId=2, name=项目经理3, rank=2, subMenus=[])])
        //People(id=1, pId=0, name=经理1, rank=2, subMenus=[People(id=4, pId=1, name=项目经理2, rank=1, subMenus=[]), People(id=3, pId=1, name=项目经理1, rank=2, subMenus=[People(id=8, pId=3, name=普工2, rank=1, subMenus=[]), People(id=7, pId=3, name=普工1, rank=2, subMenus=[])])])
        //People(id=6, pId=2, name=项目经理4, rank=1, subMenus=[])
        //People(id=5, pId=2, name=项目经理3, rank=2, subMenus=[])
        //People(id=4, pId=1, name=项目经理2, rank=1, subMenus=[])
        //People(id=3, pId=1, name=项目经理1, rank=2, subMenus=[People(id=8, pId=3, name=普工2, rank=1, subMenus=[]), People(id=7, pId=3, name=普工1, rank=2, subMenus=[])])
        //People(id=8, pId=3, name=普工2, rank=1, subMenus=[])
        //People(id=7, pId=3, name=普工1, rank=2, subMenus=[])
    }
}
